package com.singfan.core.util;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.singfan.core.LoggerFactory;
import com.singfan.core.dao.database.model.constant.PlatformType;
import org.apache.log4j.Logger;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Author: hansonghe
 * Date: 2015-09-08 16:44
 * Desc:
 */
public class PushUtil {
    private static final String CUSTOMER_MASTER_SECRET = "d33de9f56e3d4a2be0380cfd";
    private static final String CUSTOMER_APP_KEY = "016c207c188b50cca68acf31";

    private static final String BARBER_MASTER_SECRET = "502b6658afd7e962defd904c";
    private static final String BARBER_APP_KEY = "d13daf038cebc07b1fe7556c";

    private static ExecutorService executorService = Executors.newCachedThreadPool();
    private static Logger traceLog = LoggerFactory.getTraceLogger();

    public static void push(String registrationId, int userType, int platformType, String content) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    JPushClient jpushClient = getPushClient(userType);
                    if (jpushClient != null){
                        PushPayload.Builder builder = PushPayload.newBuilder()
                                .setAudience(Audience.registrationId(registrationId));

                        if (platformType == PlatformType.ANDROID){
                            builder = builder.setPlatform(Platform.android())
                                    .setNotification(Notification.android(content, null, null));;
                        }
                        else if (platformType == PlatformType.IOS){
                            builder = builder.setPlatform(Platform.ios())
                                    .setNotification(Notification.ios(content, null));
                        }
                        PushPayload pushPayload = builder.build();

                        PushResult result = jpushClient.sendPush(pushPayload);

                        traceLog.debug(String.format("PushUtil.push() successful -> registrationId:%1$s, platformType:%2$d, content:%3$s, result:%4$s", registrationId, platformType, content, result));
                    }
                    else {
                        traceLog.error(String.format("PushUtil.push() failed -> registrationId:%1$s, platformType:%2$d, content:%3$s, error:unknown useType %4$d", registrationId, platformType, content, userType));
                    }
                }
                catch (Exception e) {
                    traceLog.error(String.format("PushUtil.push() failed -> registrationId:%1$s, platformType:%2$d, content:%3$s, error:%4$s", registrationId, platformType, content, e.getMessage()));
                    e.printStackTrace();
                }
            }
        });
    }

    private static JPushClient getPushClient(int userType){
        JPushClient jpushClient = null;

        if (userType == 1){
            jpushClient = new JPushClient(CUSTOMER_MASTER_SECRET, CUSTOMER_APP_KEY, 3);
        }
        else if (userType == 2){
            jpushClient = new JPushClient(BARBER_MASTER_SECRET, BARBER_APP_KEY, 3);
        }

        return jpushClient;
    }
}
